package com.horay.statemachiedemo.multi.persist;

import com.horay.statemachiedemo.multi.enums.OrderState;
import com.horay.statemachiedemo.multi.event.OrderEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@Repository("orderMemoryStateMachinePersist")
public class OrderMemoryStateMachinePersist implements StateMachinePersist<OrderState, OrderEvent, String> {

    private Map<String, StateMachineContext<OrderState, OrderEvent>> map = new HashMap<>();

    @Override
    public void write(StateMachineContext<OrderState, OrderEvent> context, String contextObj) throws Exception {
        log.info("contextObj:[{}], context:[{}]", contextObj, context);
        map.put(contextObj, context);
    }

    @Override
    public StateMachineContext<OrderState, OrderEvent> read(String contextObj) throws Exception {
        log.info("contextObj:[{}]", contextObj);
        return map.get(contextObj);
    }

}
